package com.mongo.demo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import com.mongo.demo.pojo.User;
import com.mongo.demo.service.UserRepository;

@Component
public class UserRepositoryImpl implements UserRepository{
	
	@Autowired
	private MongoTemplate mongoTemplate;
	
	@Override
	public void saveUser(User user) {
		// TODO Auto-generated method stub
		mongoTemplate.save(user);
	}

	@Override
	public User findUserByUserName(String userName) {
		// TODO Auto-generated method stub
		Query query = new Query(Criteria.where("userName").is(userName));
		User user = mongoTemplate.findOne(query, User.class);
		return user;
	}

	@Override
	public long updateUser(User user) {
	
		return 0;
	}

	@Override
	public void deleteUserById(Long id) {
		// TODO Auto-generated method stub
		Query query = new Query(Criteria.where("id").is(id));
		mongoTemplate.remove(query, User.class);
	}

}
